<template>
  <div class="pageLevel smstemplate" id="app">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <div class="content-form">
        <el-form-item label="短信code" prop="code">
          <el-input v-model="ruleForm.code"></el-input>
          <div style="color: #c0c4cc; width:800px">提示:阿里云审核通过的短信code, 请在此写入数搌库,以方便您在活动创建页面选择发送的短信模板</div>
        </el-form-item>
        <el-form-item label="短信内容" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="签名" prop="templateName">
          <el-input v-model="ruleForm.templateName"></el-input>
        </el-form-item>
        <el-form-item label="公钥" prop="smsAccessKey">
          <el-input v-model="ruleForm.smsAccessKey"></el-input>
        </el-form-item>
        <el-form-item label="私钥" prop="smsAccessKeySecret">
          <el-input v-model="ruleForm.smsAccessKeySecret"></el-input>
        </el-form-item>
      </div>
      <el-button style=" margin-left:500px;" type="primary" @click="submitForm('ruleForm')">保存</el-button>
    </el-form>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { saveMsgTemplate } from "@/api";
export default {
  computed: mapState(["merchantId"]),
  data() {
    return {
      ruleForm: {
        tips: "00000",
        name: "",
        code: "",
        templateName: "",
        smsAccessKey: "",
        smsAccessKeySecret: ""
      },
      rules: {
        code: [{ required: true, message: "请输入短信code", trigger: "blur" }],
        name: [{ required: true, message: "请输入短信内容", trigger: "blur" }],
        tips: [{ required: true }],
        templateName: [{ required: true, message: "请输入签名", trigger: "blur" }],
        smsAccessKey: [{ required: true, message: "请输入公钥", trigger: "blur" }],
        smsAccessKeySecret: [{ required: true, message: "请输入私钥", trigger: "blur" }]
      }
    };
  },
  methods: {
    message() {
      let data = {
        orgId: this.merchantId,
        msgCode: this.ruleForm.code,
        msgContent: this.ruleForm.name,
        templateName: this.ruleForm.templateName,
        smsAccessKey: this.ruleForm.smsAccessKey,
        smsAccessKeySecret: this.ruleForm.smsAccessKeySecret
      };
      saveMsgTemplate(data).then(res => {
        this.$message({
          duration: 4000,
          type: "success",
          message: "短信模板创建成功!"
        });
        this.ruleForm.name = "";
        this.ruleForm.code = "";
        this.ruleForm.templateName = "";
        this.ruleForm.smsAccessKey = "";
        this.ruleForm.smsAccessKeySecret = "";
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.message();
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.content-form {
  width: 500px;
  margin-top: 50px;
  margin-left: 30px;
}
#app {
  text-align: left;
}
</style>
